package com.jucai.pppeiyouclient.utils;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;

import java.io.File;

/**
 * Created by Kuky on 2017-04-14.
 */

public class ImageHandleUtils {
    public static final String FILE_PROVIDER = "com.jucai.pppeiyouclient.fileprovider";

    /**
     * 拍照
     *
     * @param activity
     * @param imagePath
     * @param requestCode
     */
    public static void takePhoto(Activity activity, File imagePath, int requestCode) {
        //用于启动相机
        Intent takePhotoIntent = new Intent();
        Uri imageUri = null;

        if (!imagePath.getParentFile().exists()) {
            imagePath.getParentFile().mkdirs();
        }

        if (Build.VERSION.SDK_INT >= 24) {
            //Android 7.0以上，将file转换为一个封装过的uri，否则会抛出FileUriExposedException异常
            //FileProvider是一种特殊内容提供器，选择性将封装过的uri共享给外部，需要在Manifest注册
            imageUri = FileProvider.getUriForFile(activity, FILE_PROVIDER, imagePath);

            //对目标应用临时授权该Uri所代表的文件
            takePhotoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            //Android 7.0以下，直接将file转换为uri，为图片的本地真实路径
            imageUri = Uri.fromFile(imagePath);
        }
        takePhotoIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        //指定图片输出地址
        takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        activity.startActivityForResult(takePhotoIntent, requestCode);
    }

    /**
     * 打开相册
     */
    public static void openAlum(Activity activity, int requestCode) {
        Intent openAlumIntent;

        if (Build.VERSION.SDK_INT < 19) {
            openAlumIntent = new Intent(Intent.ACTION_GET_CONTENT);
            openAlumIntent.setType("image/*");
        } else
            openAlumIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        activity.startActivityForResult(openAlumIntent, requestCode);
    }

    /**
     * 裁剪图片方法实现
     *
     * @param uri
     */
    public static void startCropImage(Activity activity, Uri uri, int requestCode, int aspectX, int aspectY, int outputX, int outputY) {
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        //限制图片类型可设置为"image/jpeg" 或者 "image/png"
        cropIntent.setDataAndType(uri, "image/*");
        //设置裁剪
        cropIntent.putExtra("crop", "true");
        //aspectX aspectY 是宽高的比例
        if (aspectX != 0 && aspectY != 0) {
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
        }
        //outputX outputY 是裁剪图片宽高
        cropIntent.putExtra("outputX", outputX);
        cropIntent.putExtra("outputY", outputY);
        //去除面部检测
//        cropIntent.putExtra("noFaceDetection", true);
        //是否设置返回值
        cropIntent.putExtra("return-data", true);
        activity.startActivityForResult(cropIntent, requestCode);
    }

}
